package com.sky.controller.admin;

import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;

@RestController("adminDishController")
@Api(tags = "菜品相关接口")
@RequestMapping("/admin/dish")
@Slf4j
public class DishController {

    @Autowired
    private DishService dishService;

    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 新增菜品
     * @param dishDTO
     * @return
     */
    @PostMapping
    @ApiOperation("新增菜品")
    public Result<String> save(@RequestBody DishDTO dishDTO){
        log.info("新增菜品:{}", dishDTO);
        dishService.save(dishDTO);

        // 清理Redis中的对应数据
        String key = "dish_" + dishDTO.getCategoryId();
        cleanRedis(key);

        return Result.success();
    }

    @GetMapping("/page")
    @ApiOperation("菜品信息分页查询")
    public Result<PageResult> page(DishPageQueryDTO dishPageQueryDTO){
        log.info("菜品信息分页查询:{}", dishPageQueryDTO);
        PageResult pageResult = dishService.page(dishPageQueryDTO);

        return Result.success(pageResult);
    }

    /**
     * 起售/停售菜品
     * @param id
     * @return
     */
    @PostMapping("/status/{status}")
    @ApiOperation("起售/停售菜品")
    public Result<String> enableOrDisable(@PathVariable Integer status, Long id){
        log.info("起售/停售菜品:{},{}", status, id);
        DishDTO dishDTO = new DishDTO();
        dishDTO.setId(id);
        dishDTO.setStatus(status);
        dishService.update(dishDTO, false);

        // 清理Redis中的所有以dish_开头的数据
        String key = "dish_*";
        cleanRedis(key);

        return Result.success();
    }

    /**
     * 根据id获取菜品信息
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    @ApiOperation("根据id获取菜品信息")
    public Result<DishVO> getById(@PathVariable Long id){
        log.info("根据id获取菜品信息:{}", id);
        DishVO dishVO = dishService.getById(id);

        return Result.success(dishVO);
    }

    /**
     * 更新菜品信息
     * @param dishDTO
     * @return
     */
    @PutMapping
    @ApiOperation("更新菜品信息")
    public Result<String> update(@RequestBody DishDTO dishDTO){
        log.info("更新菜品信息:{}", dishDTO);
        dishService.update(dishDTO, true);

        // 清理Redis中的所有以dish_开头的数据
        String key = "dish_*";
        cleanRedis(key);

        return Result.success();
    }

    /**
     * 删除菜品信息--支持批量删除
     * @param ids
     * @return
     */
    @DeleteMapping
    @ApiOperation("删除菜品信息")
    public Result<String> delete(@RequestParam List<Long> ids){
        log.info("删除菜品信息:{}", ids);
        dishService.deleteBatch(ids);

        // 清理Redis中的所有以dish_开头的数据
        String key = "dish_*";
        cleanRedis(key);

        return Result.success();
    }

    /**
     * 根据菜品分类id/菜品名称获取菜品信息列表
     * @param categoryId
     * @return
     */
    @GetMapping("/list")
    @ApiOperation("根据菜品分类id/菜品名称获取菜品信息列表")
    public Result<List<Dish>> getByCategoryId(Long categoryId, String name){
        log.info("根据菜品分类id/菜品名称获取菜品信息列表:{},{}" ,categoryId, name);
        List<Dish> dishList = null;
        if(categoryId != null){
            dishList = dishService.getByCategoryId(categoryId);
        }else{
            dishList = dishService.getByName(name);
        }

        return Result.success(dishList);
    }

    /**
     * 清理Redis中的数据
     * @param pattern
     */
    private void cleanRedis(String pattern){
        Set keys = redisTemplate.keys(pattern);
        redisTemplate.delete(keys);
    }

}
